import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React from "react";
import CustomInput from "../../compoents/input";
import SecondaryLayout from "../../compoents/secondary-layout";
import classes from "./editProfile.module.less";
import { useDispatch, useSelector } from "react-redux";
import { matchTheme } from "../../utils";
import IconButton from "@mui/material/IconButton";
import { upload } from "../../utils/aliyun";
import useServices from "../../hooks/useRequest";
import { updateUserInfo } from "../../api/user";
import toast from "../../compoents/toast";
import { useClipboard } from "use-clipboard-copy";
import classNames from "classnames";
import { useLocalStorageSetState } from "../../hooks";
import { compressAccurately } from "image-conversion";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../local";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function EditProfile() {
    var _localStore_prevAccount;
    var dispatch = useDispatch();
    var copy = useClipboard({
        onSuccess: function onSuccess() {
            toast.copy();
        }
    }).copy;
    var _useLocalStorageSetState = _sliced_to_array(useLocalStorageSetState(), 1), localStore = _useLocalStorageSetState[0];
    var _useSelector = useSelector(function(state) {
        return {
            userInfo: state.app.userInfo,
            theme: state.app.theme
        };
    }), userInfo = _useSelector.userInfo, theme = _useSelector.theme;
    var t = useTranslation().t;
    var services = useServices({
        upload: function() {
            var _ref = _async_to_generator(function(files) {
                var res;
                return _ts_generator(this, function(_state) {
                    switch(_state.label){
                        case 0:
                            return [
                                4,
                                upload(files)
                            ];
                        case 1:
                            res = _state.sent();
                            return [
                                2,
                                res
                            ];
                    }
                });
            });
            return function(files) {
                return _ref.apply(this, arguments);
            };
        }(),
        updateUserInfo: updateUserInfo
    });
    var updateAvatar = function() {
        var _ref = _async_to_generator(function(e) {
            var files, _file;
            return _ts_generator(this, function(_state) {
                switch(_state.label){
                    case 0:
                        files = e.target.files;
                        if (!(files === null || files === void 0 ? void 0 : files.length)) return [
                            2
                        ];
                        _file = files[0];
                        if (!(files[0].size > 60000 * 1.25)) return [
                            3,
                            2
                        ];
                        return [
                            4,
                            compressAccurately(_file, 60)
                        ];
                    case 1:
                        // 雅座图片到指定大小左右
                        _file = _state.sent();
                        _state.label = 2;
                    case 2:
                        services.upload([
                            new File([
                                _file
                            ], files[0].name, {
                                type: _file.type
                            })
                        ]).then(function(urls) {
                            var avatarLink = urls[0];
                            /** 发送后端 */ services.updateUserInfo({
                                avatarLink: avatarLink
                            }).then(function(res) {
                                if (res.code == 200) {
                                    dispatch.app.SET_USER_INFO({
                                        avatarLink: avatarLink
                                    });
                                } else {
                                    toast.createToast({
                                        message: t(LocalKeyMap["toast.error.update"]),
                                        type: "errToast"
                                    });
                                }
                            }).catch(function(err) {
                                console.log(err);
                            });
                            e.target.files = null;
                        }).catch(function(err) {
                            console.log("err=>", err);
                        // e.target.files = null;
                        });
                        return [
                            2
                        ];
                }
            });
        });
        return function updateAvatar(e) {
            return _ref.apply(this, arguments);
        };
    }();
    return /*#__PURE__*/ _jsx(SecondaryLayout, {
        title: t(LocalKeyMap["editProfile.title"]),
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes.edit_profile,
            children: [
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.avatar,
                    children: [
                        /*#__PURE__*/ _jsx("img", {
                            src: userInfo.avatarLink
                        }),
                        /*#__PURE__*/ _jsxs(IconButton, {
                            component: "label",
                            className: classes.edit_icon,
                            children: [
                                /*#__PURE__*/ _jsx("input", {
                                    onChange: updateAvatar,
                                    style: {
                                        width: "0px",
                                        height: "0px",
                                        filter: "alpha(opacity=0)",
                                        opacity: 0,
                                        WebkitFilter: "alpha(opacity=0)"
                                    },
                                    className: classes.input,
                                    accept: "image/*",
                                    type: "file"
                                }),
                                /*#__PURE__*/ _jsx("img", {
                                    src: require("../../assets/icons/edit.svg")
                                })
                            ]
                        })
                    ]
                }),
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.info,
                    children: [
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.item,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.title,
                                    children: t(LocalKeyMap["editProfile.userName"])
                                }),
                                /*#__PURE__*/ _jsx("div", {
                                    onClick: function() {
                                        dispatch.app.PUSH("/setUsername");
                                    },
                                    children: /*#__PURE__*/ _jsx(CustomInput, {
                                        className: classNames(classes.input, classes.user_input),
                                        value: userInfo.nickname,
                                        suffixIcon: /*#__PURE__*/ _jsx("img", {
                                            src: matchTheme(theme, {
                                                light: require("../../assets/icons/light/rightPArrow.svg"),
                                                dark: require("../../assets/icons/dark/rightOLArrow.svg")
                                            }),
                                            className: classes.user_name_arrow
                                        }),
                                        readonly: true
                                    })
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.item,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.title,
                                    children: t(LocalKeyMap.did)
                                }),
                                /*#__PURE__*/ _jsx(CustomInput, {
                                    onClick: function() {
                                        return copy(userInfo.did);
                                    },
                                    className: classes.input,
                                    value: userInfo.did,
                                    suffixIcon: /*#__PURE__*/ _jsx("img", {
                                        src: matchTheme(theme, {
                                            light: require("../../assets/icons/copy.svg"),
                                            dark: require("../../assets/icons/dark/copy.svg")
                                        }),
                                        className: classes.copy
                                    }),
                                    readonly: true
                                })
                            ]
                        }),
                        /*#__PURE__*/ _jsxs("div", {
                            className: classes.item,
                            children: [
                                /*#__PURE__*/ _jsx("div", {
                                    className: classes.title,
                                    children: t(LocalKeyMap.loginMethod)
                                }),
                                /*#__PURE__*/ _jsx(CustomInput, {
                                    className: classes.input,
                                    value: userInfo.email || userInfo.phone || (localStore === null || localStore === void 0 ? void 0 : (_localStore_prevAccount = localStore.prevAccount) === null || _localStore_prevAccount === void 0 ? void 0 : _localStore_prevAccount.type),
                                    readonly: true
                                })
                            ]
                        })
                    ]
                })
            ]
        })
    });
}
export default EditProfile;

 //# sourceMappingURL=index.js.map